package com.shop.user.interceptor;

import com.shop.utils.common.enums.Message;
import com.shop.utils.config.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Slf4j
public class UserTokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String check = request.getHeader("userId");
        log.info("checkUserId:{}",check);
        if(check != null&&!check.equals("")){
            log.info("check:{}",check);
            //存入到当前线程中
            Long userId= Long.valueOf(check);
            log.info("userId:{}",userId);
            BaseContext.setCurrentId(userId);
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        BaseContext.removeCurrentId();
    }
}
